Attribute VB_Name = "GlobalProcedures"
Enum TabManipulate
    Add
    Remove
End Enum

Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long


Public Sub ReadConnection(txtServerName As Object, txtDataSourceName As Object, txtDatabaseName As Object, txtPassword As Object)
    Open App.Path & "\Connection.ini" For Input Access Read As #1
    Do While Not EOF(1)
      Input #1, sLine
      sLine = Trim(sLine)
        
      'Acquiring ini data
      If InStr(sLine, "HOST") Then
        lPos = InStr(sLine, "=")
        txtServerName = Trim(Right(sLine, Len(sLine) - lPos))
      ElseIf InStr(sLine, "DATASOURCE") Then
        lPos = InStr(sLine, "=")
        txtDataSourceName = Trim(Right(sLine, Len(sLine) - lPos))
      ElseIf InStr(sLine, "DATABASE") Then
        lPos = InStr(sLine, "=")
        txtDatabaseName = Trim(Right(sLine, Len(sLine) - lPos))
      ElseIf InStr(sLine, "PASSWORD") Then
        lPos = InStr(sLine, "=")
        txtPassword = Trim(Right(sLine, Len(sLine) - lPos))
      End If
    Loop
    Close #1
    sConnString = "driver={" & txtDataSourceName & "};" & "server=" & txtServerName & ";uid=sa;pwd=" & txtPassword & ";database=" & txtDatabaseName
End Sub

Public Sub AddTVNode(tmpTV As TreeView, tmpIMG As ImageList, tmpRS As Recordset)
    Dim nodX As Node
    tmpTV.Nodes.Clear
    tmpTV.ImageList = tmpIMG
    tmpTV.FullRowSelect = True
    If tmpRS.RecordCount = 0 Then Exit Sub
    Dim i As Integer
    With tmpRS
        .MoveFirst
        For i = 0 To .RecordCount - 1
            If !cParent = "" Then
                Set nodX = tmpTV.Nodes.Add(, , "P" & !cModuleID, !cDesc, Int(!nIcon))
            ElseIf !cLink = "" Then
                Set nodX = tmpTV.Nodes.Add("P" & !cParent, tvwChild, "P" & !cModuleID, !cDesc, Int(!nIcon))
            Else
                Set nodX = tmpTV.Nodes.Add("P" & !cParent, tvwChild, !cLink, !cDesc, Int(!nIcon))
            End If
            nodX.Expanded = True
            .MoveNext
        Next i
    End With
End Sub

'ex. LinkForms "Addon1.Slot1.Form1"
Public Function LinkForms(sLink As String) As Boolean
    On Error GoTo ErrHandler
    Dim a
    Dim sOBJ As Object
    Dim tmp As Boolean
    If sLink = "" Then LinkForms = False: Exit Function
    a = Split(sLink, ".")
    If UBound(a) = 0 Then Exit Function
'    a (0) dll name: a (1) class name: a (2) form name
    
    Set sOBJ = CreateObject(a(0) & ".ShowForm")
    Set sOBJ.MDI = MiniERPMain
    sOBJ.ShowForm (a(1))
    LinkForms = sOBJ.lSuccess
    If LinkForms Then Set LFrmName = sOBJ.SFrmName: Set FrmName = sOBJ.SFrmName
    Set sOBJ = Nothing
ErrHandler:
End Function


 Public Sub LoadDynamicForm(pFoRM As Object, pPaReNT As Object)
    pFoRM.Show
    SetParent pFoRM.hwnd, pPaReNT.hwnd
    pFoRM.ScaleMode = vbPixels
    MoveWindow pFoRM.hwnd, 0, 0, pFoRM.ScaleWidth, pFoRM.ScaleHeight, True
    
    ResizeChild pFoRM, pPaReNT
End Sub

Public Sub ResizeChild(pFoRM As Object, pPaReNT As Object)
    'pFoRM.Move 285, 330, pPaReNT.Width, pPaReNT.Height
    'pFoRM.Move 0, 0, pPaReNT.Width, pPaReNT.Height
    pFoRM.WindowState = 2
    pPaReNT.Height = pFoRM.Height
    pPaReNT.Width = pFoRM.Width
    Set FrmName = pFoRM
End Sub

Public Sub MainTab(eTab As TabManipulate)
    Dim i As Integer
    Dim lFind As Boolean
    Select Case eTab
        Case Add
            For i = 0 To MiniERPMain.tsMainTab.Tabs.Count - 1
                If MiniERPMain.tsMainTab.Tabs(i + 1).Key = FrmName.Name Then
                    MiniERPMain.tsMainTab.Tabs(i + 1).Selected = True
                    lFind = True
                    Exit For
                End If
            Next i
            
            If lFind = False Then
                MiniERPMain.tsMainTab.Tabs.Add , FrmName.Name, FrmName.Caption, PickImgLstIcon(FrmName.Tag)
                For i = 1 To MiniERPMain.tsMainTab.Tabs.Count
                    If MiniERPMain.tsMainTab.Tabs(i).Caption = FrmName.Caption Then
                        MiniERPMain.tsMainTab.Tabs(i).Selected = True
                    End If
                Next i
            End If
            
        Case Remove
            If MiniERPMain.tsMainTab.Tabs.Count <= 0 Then Exit Sub
            MiniERPMain.tsMainTab.Tabs.Remove MiniERPMain.tsMainTab.SelectedItem.Index
            FrmName.TBClose
    End Select
End Sub

Public Function PickImgLstIcon(sType As String) As Integer
    Select Case sType
        Case "Application"
            PickImgLstIcon = 1
        Case "Reports"
            PickImgLstIcon = 2
        Case "Settings"
            PickImgLstIcon = 3
        Case Else
            PickImgLstIcon = 4
    End Select
End Function

'Encrypting string
Public Function Encrypt(str As String) As String
Dim intCntr, intKey As Integer
Dim strTemp As String
Dim tmp As String
  
    intKey = Len(str) - 1
    strTemp = ""
    For intCntr = 0 To Len(str) - 1
        tmp = Chr(Asc(Left(Right(str, Len(str) - intCntr), 1)) - (intKey + intCntr))
        If tmp = "'" Then
            tmp = "*"
        End If
        strTemp = strTemp & tmp
        intKey = intKey + 3
    Next intCntr
    Encrypt = strTemp & IIf(Chr(intKey) <> "'", Chr(intKey), "*")
    
End Function

'Decrypting string
Public Function Decrypt(str As String) As String
Dim intCntr, intKey As Integer
Dim strTemp As String
  
    intKey = Len(str) - 2
    strTemp = ""
    For intCntr = 0 To Len(str) - 2
       If Left(Right(str, Len(str) - intCntr), 1) = "*" Then
            strTemp = strTemp & Chr(Asc("'") + (intKey + intCntr))
       Else
            strTemp = strTemp & Chr(Asc(Left(Right(str, Len(str) - intCntr), 1)) + (intKey + intCntr))
       End If
        intKey = intKey + 3
    Next intCntr
    Decrypt = strTemp
    
End Function

